public class Solution92 {
    ListNode r;

    public ListNode reverseBetween(ListNode head, int left, int right) {
        head = new ListNode(0, head);
        int i = 0;
        ListNode p = head;
        while (p != null) {
            if (i == left - 1) {
                ListNode t = reverse(p, p.next, right - left + 1);
                t.next = r;
                break;
            }
            p = p.next;
            i++;
        }
        return head.next;
    }

    ListNode reverse(ListNode head, ListNode first, int n) {
        if (n == 1) {
            r = first.next;
            head.next = first;
            return first;
        }
        reverse(head, first.next, n - 1).next = first;
        return first;
    }
}
